package com.it2297.view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JSeparator;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
import javax.swing.border.MatteBorder;

import com.it2297.controller.CompanyController;
import com.it2297.main.CompanyApp;

public class CompanyUpdateCompany extends JFrame {

	private JMenuBar menuBar;
	private JMenu menuHome;
	private JMenu menuExit;
	private JMenu menuJobAd;
	private JMenuItem menuRemoveJob;
	private JMenuItem menuCreateJob;
	private JMenuItem menuViewAllJobsByCompany;
	private JMenu menuApplicants;
	private JMenuItem menuViewApplicants;
	private JSeparator separator;
	private Component horizontalGlue;
	private Component horizontalStrut;
	private JLabel lblWelcome;
	private JMenuItem menuLogout;
	private JMenuItem menuhomescreen; 
	private JMenu mnProfile;
    private JMenuItem mntmUpdateCompany;
	
	
	 private JPanel contentPane;
	 private JTextField tb_email;
     private JTextField tb_phone; 
     private JComboBox comboBox;
     private JPasswordField pwdfield;
     private String companyName;
     final CompanyController cct= new CompanyController(this); 
     private JTextField tf_rqn;
     private JTextField tf_rans;
     private JTextArea tb_address;
     private JTextArea tb_desc;
     private JTextField tb_name;

     public String getCompanyName(){
           //  return comboBox.getSelectedItem().toString(); 
    	 return tb_name.getText();
     } 
     
     public String getPassword(){
             String password="";
             for(int i=0;i<pwdfield.getPassword().length;i++){
                     password +=pwdfield.getPassword()[i]; 
             }
         return password;
     }

     public String getEmail(){
         return tb_email.getText();
     }

     public String getAddress(){
         return tb_address.getText();
     }

     public int getPhone(){
             int phonenum = Integer.parseInt(tb_phone.getText());
             return phonenum;
     }

     public String getDescription(){
             return tb_desc.getText();
     }
     
     public String getRecoveryQuestion(){
     	return tf_rqn.getText();
     }
     
     public String getRecoveryAnswer(){
     	return tf_rans.getText();
     }

     public void setCompanyName(String companyName){
             this.companyName = companyName;
     }

     public void setPassword(String password){
             pwdfield.setText(password);
     }

     public void setEmail(String email){
             tb_email.setText(email);
     }

     public void setAddress(String address){
             tb_address.setText(address);
     }

     public void setPhone(String phone){
             tb_phone.setText(phone);
     }

     public void setDescription(String description){
             tb_desc.setText(description);
     }
     
     public void setRecoveryQuestion(String rqn){
     		tf_rqn.setText(rqn);
     }
     
     public void setRecoveryAnswer(String rans){
     		tf_rans.setText(rans);
     }

	/**
	 * Launch the application.
	 */
	/*public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					CompanyUpdateCompany frame = new CompanyUpdateCompany();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
*/
	/**
	 * Create the frame.
	 */
	public CompanyUpdateCompany() {
		//setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 740, 560);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		getContentPane().setLayout(null);        
		setLocationRelativeTo(null);
         contentPane.setLayout(null);
		
         JLabel lblNewLabel = new JLabel("Update Company Information");
         lblNewLabel.setFont(new Font("Times New Roman", Font.BOLD, 35));
         lblNewLabel.setBounds(10, 11, 720, 55);
         getContentPane().add(lblNewLabel);           

         JLabel lblCompanyName = new JLabel("Company Name:");
         lblCompanyName.setFont(new Font("Tahoma", Font.BOLD, 14));
         lblCompanyName.setBounds(10, 65, 188, 24);
         getContentPane().add(lblCompanyName);

         JLabel lblNewLabel_2 = new JLabel("Company Email:"); 
         lblNewLabel_2.setFont(new Font("Tahoma", Font.BOLD, 14));
         lblNewLabel_2.setBounds(10, 330, 188, 24);
         getContentPane().add(lblNewLabel_2);              

         JLabel lblNewLabel_3 = new JLabel("Company Address:");
         lblNewLabel_3.setFont(new Font("Tahoma", Font.BOLD, 14));
         lblNewLabel_3.setBounds(10, 100, 188, 24);
         getContentPane().add(lblNewLabel_3);

         JLabel lblNewLabel_4 = new JLabel("Company Phone Number:");
         lblNewLabel_4.setFont(new Font("Tahoma", Font.BOLD, 14));
         lblNewLabel_4.setBounds(10, 293, 188, 24);
         getContentPane().add(lblNewLabel_4);             

         JLabel lblNewLabel_5 = new JLabel("Company Description:");
         lblNewLabel_5.setFont(new Font("Tahoma", Font.BOLD, 14));
         lblNewLabel_5.setBounds(10, 185, 188, 24);
         getContentPane().add(lblNewLabel_5);            

         tb_email = new JTextField();
         tb_email.setText(CompanyApp.getCurrentUser().getEmail());
         tb_email.setBounds(208, 331, 296, 26);
         getContentPane().add(tb_email);
         tb_email.setColumns(10);

         tb_phone = new JTextField();
         tb_phone.setText(Integer.toString(CompanyApp.getCurrentUser().getPhone()));
         tb_phone.setBounds(208, 294, 130, 26);
         getContentPane().add(tb_phone);
         tb_phone.setColumns(10);
         
     /*    comboBox = new JComboBox();
         comboBox.addItem("------------------------------------------Select a company name---------------------------------------");
	                for(int i=0; i<cct.retrieveCompanyName().size();i++){
	                comboBox.addItem(cct.retrieveCompanyName().get(i));
	               }
         comboBox.addActionListener(new ActionListener() {
                 public void actionPerformed(ActionEvent e) {
                         cct.retrieveFullCompanyDetails();
                         if(comboBox.getSelectedItem()==("------------------------------------------Select a company name---------------------------------------")){
                                tb_phone.setText(null);
                         }            
                 }
         });
         comboBox.setBounds(58, 239, 463, 26);
         getContentPane().add(comboBox);
*/
         JButton btnNewButton = new JButton("Update");
         btnNewButton.addActionListener(new ActionListener() {
                 public void actionPerformed(ActionEvent e) {                 
                         try{
                                 String password = "";
                                 for (int i = 0; i < pwdfield.getPassword().length; i++) {
                                 		password += pwdfield.getPassword()[i];
                                 }
                                         if(tb_email.getText().isEmpty() || password.equals("")|| tb_phone.getText().isEmpty() || tb_address.getText().isEmpty()||tb_desc.getText().isEmpty()||tf_rqn.getText().equals("")||tf_rans.getText().equals("")){
                                                 JOptionPane.showMessageDialog(null,"Please fill in all the necessary details.");
                                         }
                                         else{
                                        	 if(tb_phone.getText().length()>8){
                                        		 JOptionPane.showMessageDialog(null, "Phone number has more than 8 digits!");
                                        	 }
                                        	 else{             
                                        		 cct.UpdateCompany();
                                        	 }
                                       //  CompanyApp.getCurrentUser().getCompanyName();
                                      //   cct.retrieveFullCompanyDetails();
                                         
                                    //     comboBox.setSelectedItem("------------------------------------------Select a company name---------------------------------------");
                                    /*     pwdfield.setText("");
                                         tb_email.setText("");
                                         tb_phone.setText("");
                                         tb_address.setText("");
                                         tb_desc.setText("");
                                         tf_rqn.setText("");
                                         tf_rans.setText("");*/
                                         }
                         }
                         catch(NumberFormatException ex){
                                         JOptionPane.showMessageDialog(null,"Please provide a proper phone number.");
                         }
                 }
         });
         btnNewButton.setBounds(208, 479, 109, 26);
         btnNewButton.setFont(new Font("Times New Roman", Font.BOLD, 18));
         getContentPane().add(btnNewButton);
         
         JLabel lblPwd = new JLabel("Password:"); 
         lblPwd.setFont(new Font("Tahoma", Font.BOLD, 14)); 
         lblPwd.setBounds(10, 365, 188, 24);
         getContentPane().add(lblPwd);             

         pwdfield = new JPasswordField();
         pwdfield.setText(CompanyApp.getCurrentUser().getPassword());
         pwdfield.setBounds(208, 368, 235, 26);
         getContentPane().add(pwdfield);
         
         JLabel lblNewLabel_1 = new JLabel("Recovery Question:");
         lblNewLabel_1.setFont(new Font("Tahoma", Font.BOLD, 15));
         lblNewLabel_1.setBounds(10, 404, 188, 24);
         getContentPane().add(lblNewLabel_1);
         
         JLabel lblNewLabel_6 = new JLabel("Recovery Answer:");
         lblNewLabel_6.setFont(new Font("Tahoma", Font.BOLD, 15));
         lblNewLabel_6.setBounds(10, 444, 188, 24);
         getContentPane().add(lblNewLabel_6);
         
         tf_rqn = new JTextField();
         tf_rqn.setText(CompanyApp.getCurrentUser().getRecoveryQuestion());
         tf_rqn.setBounds(208, 405, 455, 26);
         getContentPane().add(tf_rqn);
         tf_rqn.setColumns(10);
         
         tf_rans = new JTextField();
         tf_rans.setText(CompanyApp.getCurrentUser().getRecoveryAnswer());
       //  tf_rans.setText("");
         tf_rans.setBounds(208, 442, 403, 26);
         getContentPane().add(tf_rans);
         tf_rans.setColumns(10);
         
         tb_address = new JTextArea();
         tb_address.setBorder(new MatteBorder(1, 1, 1, 1, (Color) new Color(0, 0, 0)));
         tb_address.setText(CompanyApp.getCurrentUser().getAddress());
         tb_address.setBounds(208, 102, 277, 78);
         getContentPane().add(tb_address);
         
         tb_desc = new JTextArea();
         tb_desc.setBorder(new MatteBorder(1, 1, 1, 1, (Color) new Color(0, 0, 0)));
         tb_desc.setText(CompanyApp.getCurrentUser().getDescription());
         tb_desc.setBounds(208, 187, 487, 96);
         getContentPane().add(tb_desc);
         
         tb_name = new JTextField();
         tb_name.setText(CompanyApp.getCurrentUser().getCompanyName());
         tb_name.setEditable(false);
         tb_name.setBounds(208, 66, 361, 26);
         contentPane.add(tb_name);
         tb_name.setColumns(10);
		
		
     	
         menuBar = new JMenuBar();
 		menuBar.setBounds(0, 0, 724, 24);
 		contentPane.add(menuBar);
 		
 		menuHome = new JMenu("Home");
 		menuBar.add(menuHome);
 		menuhomescreen=new JMenuItem("Home Screen");
 		menuhomescreen.addActionListener(new ActionListener() {
 			public void actionPerformed(ActionEvent e) {
 				CompanyHomePage chp=new CompanyHomePage();
 				chp.setLocationRelativeTo(null);
 				chp.setVisible(true);
 				chp.setTimer("start");
 				chp.setTitle("Company - Home Screen");
 				closeWindow();
 			}
 		});
 		menuHome.add(menuhomescreen);
 		
 		menuJobAd=new JMenu("Job Advertisement");
 		menuBar.add(menuJobAd);
 		menuCreateJob=new JMenuItem("Create Job Advertisement");
 		menuCreateJob.addActionListener(new ActionListener() {
 			public void actionPerformed(ActionEvent e) {
 				JobCreateJob jcj=new JobCreateJob();
 				jcj.setLocationRelativeTo(null);
 				jcj.setVisible(true);
 				jcj.setTitle("Company - Create Job Advertisement ");
 				closeWindow();
 			}
 		});
 		menuJobAd.add(menuCreateJob);
 		
 		JSeparator separator_1 = new JSeparator();
 		menuJobAd.add(separator_1);
 		menuRemoveJob=new JMenuItem("Remove Job Advertisement");
 		menuRemoveJob.addActionListener(new ActionListener() {
 			public void actionPerformed(ActionEvent e) {
 				JobRemoveJob jrj=new JobRemoveJob();
 				jrj.setLocationRelativeTo(null);
 				jrj.setVisible(true);
 				jrj.setTitle("Company - Remove Job Advertisement ");
 				closeWindow();
 			}
 		});
 		menuViewAllJobsByCompany=new JMenuItem("View All Jobs By Company");
 		menuViewAllJobsByCompany.addActionListener(new ActionListener() {
 			public void actionPerformed(ActionEvent e) {
 				CompanyViewAllJobsByCompany cvajbc=new CompanyViewAllJobsByCompany();
 				cvajbc.setLocationRelativeTo(null);
 				cvajbc.setVisible(true);
 				cvajbc.setTitle("Company - View All Jobs By Company ");
 				closeWindow();
 			}
 		});
 		menuJobAd.add(menuViewAllJobsByCompany);
 		
 		JSeparator separator_2 = new JSeparator();
 		menuJobAd.add(separator_2);
 		menuJobAd.add(menuRemoveJob);
 		
 		horizontalGlue = Box.createHorizontalGlue();
 		menuBar.add(horizontalGlue);
 		
 		
 		
 		lblWelcome = new JLabel();
 		lblWelcome.setForeground(new Color(0, 0, 128));
 		if (CompanyApp.isUserLogin())
 		{
 			lblWelcome.setText("Welcome, " + CompanyApp.getCurrentUser().getCompanyName() + ".");
 		}
 		else
 		{
 			lblWelcome.setText("Welcome, Guest! Please Login.");
 		}
 		menuBar.add(lblWelcome);
 		
 		horizontalStrut = Box.createHorizontalStrut(20);
 		menuBar.add(horizontalStrut);
 		
 		mnProfile = new JMenu("Profile");
 		menuBar.add(mnProfile);
 		
 		mntmUpdateCompany = new JMenuItem("Update Company");
 		mntmUpdateCompany.addActionListener(new ActionListener() {
 			public void actionPerformed(ActionEvent arg0) {
 				CompanyUpdateCompany cuc = new CompanyUpdateCompany();
 				cuc.setLocationRelativeTo(null);
 				cuc.setVisible(true);
 				cuc.setTitle("Company - Update Company Details");
 				closeWindow();
 			}
 		});
 		
 		JMenuItem mntmViewCompany = new JMenuItem("View Company");
 		mntmViewCompany.addActionListener(new ActionListener() {
 			public void actionPerformed(ActionEvent arg0) {
 				CompanyViewDetails cvd= new CompanyViewDetails();
 				cvd.setLocationRelativeTo(null);
 				cvd.setVisible(true);
 				cvd.setTitle("Company - View Company Details");
 				closeWindow();
 			}
 		});
 		mnProfile.add(mntmViewCompany);
 		
 		JSeparator separator_3 = new JSeparator();
 		mnProfile.add(separator_3);
 		mnProfile.add(mntmUpdateCompany);
 		
 		
 		menuExit = new JMenu("Exit");
 		menuBar.add(menuExit);
 		menuLogout=new JMenuItem("Logout");
 		menuLogout.addActionListener(new ActionListener() {
 			public void actionPerformed(ActionEvent e) {
 				CompanyApp.logoutUser();
 				
 				CompanyLogin loginUI = new CompanyLogin();
 				loginUI.setLocationRelativeTo(null);
 				loginUI.setVisible(true);
 				closeWindow();
 			}
 		});
 		menuExit.add(menuLogout);
	}
	
	public void closeWindow(){
		this.dispose();
	}
}
